Skip to content

Conversation

@nickanderson
Copy link
Member

I wasn't looking at the ticket prior to implementation, likely missing things.

Ticket: CFE-3653

@olehermanse olehermanse self-requested a review November 12, 2025 19:50
@olehermanse olehermanse requested a review from larsewi January 5, 2026 13:53
Copy link
Contributor

@larsewi larsewi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GJ 🚀


def _validate_state(self, value):
if value not in ("enabled", "disabled", "installed", "removed"):
raise ValidationError("state attribute must be 'enabled', 'disabled', 'installed', or 'removed'")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValidationError("state attribute must be 'enabled', 'disabled', 'installed', or 'removed'")
raise ValidationError("State attribute must be 'enabled', 'disabled', 'installed', or 'removed'")

Comment on lines 34 to 35
self.add_attribute("stream", str, required=False)
self.add_attribute("profile", str, required=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add validators here like for the state attribute?

Comment on lines 146 to 152
# Check the module state
if module.status == "enabled":
return "enabled"
elif module.status == "disabled":
return "disabled"
elif module.status == "installed":
return "installed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified a bit

Suggested change
# Check the module state
if module.status == "enabled":
return "enabled"
elif module.status == "disabled":
return "disabled"
elif module.status == "installed":
return "installed"
# Check the module state
if module.status in ("enabled", "disabled", "installed"):
return module.status

module_base.enable([module_spec])
module_base.base.resolve()
module_base.base.do_transaction()
self.log_verbose(f"Module {module_spec} enabled successfully")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repaired should print info log messages, I think 🤔

else:
return self._remove_module(module_base, module_spec)

except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this try/except since it will hide all bugs. The Promise type already handles any unexpected exceptions here

def _handle_evaluate(self, promiser, attributes, request):
.

Also try to keep only the code that throws exceptions inside the try block for readability. Also only catch the Exceptions that you expect. And not all possible ones with except Exception.

I wasn't looking at the ticket prior to implementation, likely missing things.

Ticket: CFE-3653
- Improve state validation error message.
- Move validation to attribute validators.
- Simplify state checking logic.
- Use info logging for repairs.
- Remove redundant try/except blocks.
- Add test and documentation files.
Copy link
Contributor

@larsewi larsewi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments 😉

"dependencies": [],
"test_command": "python3 test_dnf_appstream.py",
"version": "0.0.1"
} No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

# If we get here, module is not found or not in the specified stream
return "removed"

except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exceptions do you expect here? Maybe you can handle the ones you expect and not all exceptions? If you don't expect any exceptions, then please remove the try/catch here and other places :)



if __name__ == "__main__":
DnfAppStreamPromiseTypeModule().start() No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DnfAppStreamPromiseTypeModule().start()
DnfAppStreamPromiseTypeModule().start()

from cfengine_module_library import ValidationError
except ImportError as e:
print(f"Import error: {e}")
print("Make sure cfengine_module_library.py is in the correct location")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleading if it is dnf_appstream that fails to import

Comment on lines +23 to +31
# Test valid attributes
try:
module.validate_promise("nodejs", {
"state": "enabled",
"stream": "12"
}, {})
print(" ✓ Valid attributes validation passed")
except Exception as e:
print(f" ✗ Valid attributes validation failed: {e}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the pytest framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants